package com.cxd.performance.utils;

import com.cxd.performance.enums.DatePatternEnum;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Author: Calvin
 * Date: 2019-08-30 8:40
 * Description: 时间操作 工具类
 */
public class LocalDateUtils {

    private LocalDateUtils() {

    }

    public static String date2String(Date date,String pattern) {

        if (null == date) {
            return null;
        }
        pattern = pattern = BaseStringUtils.isEmpty(pattern) ? DatePatternEnum.PATTERN_1.getValue() : pattern;
        LocalDateTime localDateTime = asLocalDateTime(date);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        String localDateTimeStr = localDateTime.format(dateTimeFormatter);
        return localDateTimeStr;
    }

    public static Date string2Date(String localDateTimeStr,String pattern) {

        if (BaseStringUtils.isEmpty(localDateTimeStr)) {
            return null;
        }
        pattern = pattern = BaseStringUtils.isEmpty(pattern) ? DatePatternEnum.PATTERN_1.getValue() : pattern;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(localDateTimeStr, dateTimeFormatter);
        Date date = asDate(localDateTime);
        return date;
    }

    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static void main(String[] args) {
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_1.getValue()));
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_2.getValue()));
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_3.getValue()));
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_4.getValue()));
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_5.getValue()));
        System.out.println(date2String(new Date(), DatePatternEnum.PATTERN_6.getValue()));
        System.out.println("=====================================");
        System.out.println(string2Date("2019-08-26 15:38:47", DatePatternEnum.PATTERN_1.getValue()));
//        System.out.println(string2Date("2019-08-26 15:38:00", PATTERN_2)); //false
//        System.out.println(string2Date("2019-08-26 15:38:47", PATTERN_3)); //false
//        System.out.println(string2Date("2019-08-26 15:38:47", PATTERN_4)); //false
//        System.out.println(string2Date("2019-08-26 15:38:47", PATTERN_5)); //false
//        System.out.println(string2Date("2019-08-26 15:38:47", PATTERN_6)); //false
    }
}
